package org.demo.modules.system.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.demo.common.api.vo.Result;
import org.demo.common.constant.CommonConstant;
import org.demo.common.system.util.JwtUtil;
import org.demo.common.util.RedisUtil;
import org.demo.modules.system.model.SysLoginModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sys")
@Api(tags = "用户登录")
@Slf4j
public class LoginController {

    @Autowired
    private RedisUtil redisUtil;
    @ApiOperation("登录接口-调用api验证接口")
    @RequestMapping(value = "/apiToken", method = RequestMethod.POST)
    public Result<JSONObject> apiLogin(@RequestBody SysLoginModel sysLoginModel) {
        Result<JSONObject> result = new Result<>();
        // 获取用户部门信息
        JSONObject obj = new JSONObject();
        // 生成token
        String token = JwtUtil.sign(sysLoginModel.getUsername(), sysLoginModel.getPassword());
        // 设置token缓存有效时间
        redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
        redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);
        obj.put("token", token);
        result.setResult(obj);
        result.success("验证成功");
        return result;
    }
}
